﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Main_Register : ValidateBase
{
    UsersDao ud = new UsersDao();
    UsersVo uv = new UsersVo();
    DataTable dt = null;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        if (txtVail.Text != base.strValidate)
        {
            string script = "alert('验证码错误！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            return;
        }
        bool flag = CheckSpecialWord(txtname);
        if (flag)
        {
            string script = "alert('用户名不能含有特殊字符！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            return;
        }
        uv.Uname = txtname.Text;
        uv.Upwd = txtpwd.Text;
        uv.Uemail = txtemail.Text;
        string str = ud.CheckName(txtname.Text);
        if (str == "对不起，用户名已存在！")
        {
            string script = "alert('对不起，用户名已存在！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            return;
        }
        if (txtname.Text.Length < 4 || txtname.Text.Length > 16)
        {
            string script = "alert('用户名须在4-16位！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            return;
        }
        if (txtpwd.Text == "" || txtname.Text == "" || txtpwdagain.Text == "" || txtemail.Text == "")
        {
            string script = "alert('用户名密码和邮箱不能为空！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
            return;
        }
        Exception ex = new Exception();
        ex = ud.addUsers(uv);
        if (ex == null)
        {
            SetUserFolder(txtname.Text);
            Session.Remove("pwd");
            uv = ud.UsersLogin(txtname.Text, txtpwd.Text);
            Session.Add("user", uv);
            string script = "alert('注册成功！');location.href='../index.aspx';";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
        else
        {
            string script = "alert('注册失败！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "", script, true);
        }
    }

    protected void txtname_TextChanged(object sender, EventArgs e)
    {
        if (txtname.Text.Length < 4 || txtname.Text.Length > 16)
        {
            labcheck.Text = "<h3>*</h3>用户名须在<h3>4-16</h3>位，可以使用<h3>小写英文字母、数字以及中文</h3>组合。";
        }
        else
        {
            bool flag = CheckSpecialWord(txtname);
            if (flag)
            {
                labcheck.Text = "用户名不能含有特殊字符！";
            }
            else
            {
                string str = ud.CheckName(txtname.Text);
                if (str == "对不起，用户名已存在！")
                {
                    labcheck.Text = "对不起，用户名已存在！";
                }
                else
                {
                    labcheck.Text = "恭喜，用户名可用！";
                }
                Session.Add("pwd", txtpwd.Text);
                if (Session["pwd"] != null)
                {
                    txtpwd.Attributes.Add("value", Session["pwd"].ToString());
                    txtpwdagain.Attributes.Add("value", Session["pwd"].ToString());
                }
            }
        }
    }

    public bool SetUserFolder(string username)
    {
        bool result = true;
        string SpacePath = HttpContext.Current.Server.MapPath("../images/users/" + username);
        try
        {
            if (!Directory.Exists(SpacePath))
            {
                if (Directory.CreateDirectory(SpacePath) == null) result = false;
            }
        }
        catch
        {
            return false;
        }
        return result;
    }

    public bool CheckSpecialWord(TextBox txtbox)
    {
        bool flag = false;
        ArrayList list = new ArrayList();
        list.Add(" ");
        list.Add("~");
        list.Add("!");
        list.Add("@");
        list.Add("#");
        list.Add("$");
        list.Add("%");
        list.Add("^");
        list.Add("&");
        list.Add("*");
        list.Add("(");
        list.Add(")");
        list.Add("_");
        list.Add("+");
        list.Add("<");
        list.Add(">");
        list.Add(",");
        list.Add(".");
        list.Add("/");
        list.Add("\\");
        list.Add("?");
        list.Add(";");
        for (int i = 0; i < txtbox.Text.Length; i++)
        {
            if (list.Contains(txtbox.Text[i].ToString()))
            {
                flag = true;
            }
        }
        return flag;
    }
}
